//
//  SYNetWorkTools.swift
//  YANAWeibo
//
//  Created by mac on 16/5/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import AFNetworking

//请求方式
enum RequestType: Int {
    case GET = 0
    case POST = 1
}

class SYNetWorkTools: AFHTTPSessionManager {
    static let sharedTools: SYNetWorkTools = {
        let tools = SYNetWorkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    //封装网络请求
    
    func request(requestType: RequestType, url: String, param: AnyObject?, callBack: (response: AnyObject?, error: NSError?) -> () ) {
        
        let successClosure = { (dataTask: NSURLSessionDataTask, response: AnyObject?) -> Void in
            //成功的回调
            callBack(response: response, error: nil)
        }
        
        let failureClosure = { (dataTask: NSURLSessionDataTask?, error: NSError) -> Void in
            //失败的回调
            callBack(response: nil, error: error)
        }
        
        if requestType == .GET {
            GET(url, parameters: param, progress: nil, success: successClosure, failure: failureClosure)
        }else{
            POST(url, parameters: param, progress: nil, success: successClosure, failure: failureClosure)
            
        }
    }
    
    // 封装上传请求
    
    func requestUpload(url: String, params: AnyObject, data: NSData, name: String, callBack: (response: AnyObject?, error: NSError?)->()) {
        
        POST(url, parameters: params, constructingBodyWithBlock: { (formData) -> Void in
            formData.appendPartWithFileData(data, name: name, fileName: "test", mimeType: "application/octet-stream")
            }, progress: nil, success: { (_, response) -> Void in
                callBack(response: response, error: nil)
            }) { (_, error) -> Void in
                callBack(response: nil, error: error)
        }
    }
}

// 发送微博相关接口
extension SYNetWorkTools {
    // 发送图片微博接口
    func upload(accessToken: String, status: String, image: UIImage, callBack: (response: AnyObject?, error: NSError?)->()) {
        let url = "https://upload.api.weibo.com/2/statuses/upload.json"
        let params = [
            "access_token": accessToken,
            "status": status
        ]
//        let data = UIImageJPEGRepresentation(image, 0.9)!
        let data = UIImagePNGRepresentation(image)!
        requestUpload(url, params: params, data: data, name: "pic", callBack: callBack)
    }

    // 发送文字微博接口
    func update(accessToken: String, status: String, callBack: (response: AnyObject?, error: NSError?)->()) {
        let url = "https://api.weibo.com/2/statuses/update.json"
        let params = [
            "access_token": accessToken,
            "status": status
        ]
        request(.POST, url: url, param: params, callBack: callBack)
    }
}

//  首页相关接口
extension SYNetWorkTools {
    //  请求微博首页数据
    func requestStatus(accessToken: String, maxId: Int64 = 0, sinceId: Int64 = 0,  callBack: (response: AnyObject?, error: NSError?)->()) {
        //  创建url
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        //  设置参数
        let params = [
            "access_token": accessToken,
            "max_id": "\(maxId)",
            "since_id": "\(sinceId)"
        ]
        print(url + "?access_token=\(accessToken)")
        
        request(.GET, url: url, param: params, callBack: callBack)
        
    }
}



//OAuth相关接口
extension SYNetWorkTools {
    
    func requestAccessToken(code: String, callBack: (response: AnyObject?, error: NSError?) -> ()) {
        let url = "https://api.weibo.com/oauth2/access_token"
        let dict = [
            "client_id": WeiboAppKey,
            "client_secret": WeiboAppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": WeiboRedirect_uri
        ]
        request(.POST, url: url, param: dict, callBack: callBack)
    }
    
    
    //获取用户信息
    func requestedUserInfo(userAccount: SYUserAccount, callBack: (response: AnyObject?, error: NSError?) -> ()) {
        let url = "https://api.weibo.com/2/users/show.json"
        //设置参数
        let parmams = [
            "access_token": userAccount.access_token!,
            "uid": "\(userAccount.uid)"
        ]
       request(.GET, url: url, param: parmams, callBack: callBack)
    }
}







